//
//  ProductDetailRichTextCell.m
//  BaseProject
//
//  Created by mac on 2021/3/9.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "ProductDetailRichTextCell.h"
#import "CommentRichTextModel.h"
#import <SDImageCache.h>
#import "GKPhotoBrowser.h"
@interface ProductDetailRichTextCell ()
@property(nonatomic,strong)YYLabel *titleLab;
@property(nonatomic,strong)NSMutableArray<UIImageView *> *itemsArray;
@property(nonatomic,assign)BOOL loading;//cell没有复用 所以考虑一次即可
@end

@implementation ProductDetailRichTextCell
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = WhiteColor;
        self.contentView.backgroundColor = WhiteColor;
    }
    return self;
}
- (void)setRichTextModel:(CommentRichTextModel *)richTextModel
{
    _richTextModel = richTextModel;
    if (self.loading) {
        return;
    }
    WK(weakSelf)
    
    self.titleLab.attributedText = richTextModel.richContentAttriteStr;
    self.titleLab.frame = richTextModel.richTextFrame;
    
    //图片
    [self.itemsArray enumerateObjectsUsingBlock:^(UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self.itemsArray removeAllObjects];
    CGFloat orinY = richTextModel.richImageModulesTop;
    CGFloat itemWidth = WIDTH;
    for (NSInteger index = 0; index < richTextModel.images.count; index ++) {
        //imageView
        UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(0, orinY, itemWidth, richTextModel.images[index].itemHeight)];
        [self addSubview:view];
        view.contentMode = UIViewContentModeScaleAspectFill;
        view.clipsToBounds = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickItemView:)];
        view.userInteractionEnabled = YES;
        [view addGestureRecognizer:tap];
        view.tag = index;
        orinY = view.bottom;
        [self.itemsArray addObject:view];
    }
    //得收集完图片才可以回调哦
    for (NSInteger index = 0; index < richTextModel.images.count; index ++) {
        UIImageView *view = self.itemsArray[index];
        //url
        NSString *url = richTextModel.images[index].url;
        [view sd_setImageWithURL:url.wppURL placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            if (image && !richTextModel.allImagesCache) {
                richTextModel.images[index].itemHeight = view.width * image.size.height / image.size.width;
                //NSLog(@"加载回调:%f",richTextModel.images[index].itemHeight);
                [weakSelf refreshUI];
            }
        }];
    }
    self.loading = YES;
}
- (void)refreshUI
{
    CGFloat orinY = self.richTextModel.richImageModulesTop;
    for (NSInteger index = 0; index < self.itemsArray.count; index ++) {
        UIImageView *view = self.itemsArray[index];
        view.top = orinY;
        view.height = self.richTextModel.images[index].itemHeight;
        orinY = view.bottom;
        //NSLog(@"%ld %f %f %f  %f",index,self.richTextModel.richImageModulesTop,orinY,view.width,view.height)
    }
    if ([self.delegate respondsToSelector:@selector(productDetailRichTextCellBackHeight:)]) {
        //NSLog(@"回调22222222 %f",orinY);
        [self.delegate productDetailRichTextCellBackHeight:orinY];
    }
}
#pragma mark - click
- (void)clickItemView:(UITapGestureRecognizer *)sender
{
    NSMutableArray *photos = [NSMutableArray new];
    [self.richTextModel.images enumerateObjectsUsingBlock:^(CommonMediaModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        GKPhoto *photo = [GKPhoto new];
        photo.url = obj.url.wppURL;
        [photos addObject:photo];
    }];
    
    GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:photos currentIndex:sender.view.tag];
    browser.showStyle = GKPhotoBrowserShowStyleNone;
    browser.hideStyle = GKPhotoBrowserHideStyleZoomScale;
    [browser showFromVC:self.parentVC];
}
#pragma mark - lazy懒加载
- (YYLabel *)titleLab
{
    if (!_titleLab) {
        YYLabel *lab = [[YYLabel alloc]init];
        [self addSubview:lab];
        lab.numberOfLines = 0;
        _titleLab = lab;
    }
    return _titleLab;
}
- (NSMutableArray<UIImageView *> *)itemsArray
{
    if (!_itemsArray) {
        _itemsArray = [NSMutableArray array];
    }
    return _itemsArray;
}
@end
